/*
 * Proto Streamer
 * Copyright (C) 2009 John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package p.c;

import p.c.http.Connection;
import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.Message;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;

/**
 * An HTTP POST request.
 * @author jdp
 */
public class Writer
    extends Client
{
    public final static String Op = "POST";


    protected final Listener listener;

    private volatile Message[] list;
    private final Object monitor = new Object();


    public Writer(URL url, Listener listener)
        throws java.net.ProtocolException
    {
        super(Op,url,listener);
        this.listener = listener;
        this.connection.setRequestMethod(Op);
    }

    public void deliver(Message m){
        if (null != m){
            synchronized(this.monitor){
                this.list = Add(this.list,m);
                this.monitor.notify();
            }
        }
    }
    private Message[] delivery()
        throws InterruptedException
    {
        synchronized(this.monitor){
            while (true){
                Message[] swap = this.list;
                if (null == swap)
                    this.monitor.wait();
                else
                    return swap;
            }
        }
    }

    public void run(){
        Listener listener = this.listener;
        Mux mux = this.mux;
        try {
            CodedOutputStream out = CodedOutputStream.newInstance(this.connection.getOutputStreamer());
            mux.enter(this,out);
            listener.clientEnter(this);
            while (true){
                Message[] list = this.delivery();
                for (Message m : list)
                    mux.write(m,out);
            }
        }
        catch (InterruptedException inx){
            return;
        }
        catch (IOException exc){
            PrintStream out = System.err;
            synchronized(out){
                out.print(this.getName()+' ');
                exc.printStackTrace(out);
            }
        }
        finally {
            mux.exit(this);
            listener.clientExit(this);
        }
    }


    private final static Message[] Add(Message[] list, Message m){
        if (null == list)
            return new Message[]{m};
        else {
            int len = list.length;
            Message[] copy = new Message[len+1];
            System.arraycopy(list,0,copy,0,len);
            copy[len] = m;
            return copy;
        }
    }
}
